2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCER_TEXTEDITORHANDLER_JUCEHEADER__
27 #define __JUCER_TEXTEDITORHANDLER_JUCEHEADER__
30 //==============================================================================
33 class TextEditorHandler
: public ComponentTypeHandler
36 //==============================================================================
38 : ComponentTypeHandler ("Text Editor", "TextEditor", typeid (TextEditor
), 150, 24)
40 registerColour (TextEditor::textColourId
, "text", "textcol");
41 registerColour (TextEditor::backgroundColourId
, "background", "bkgcol");
42 registerColour (TextEditor::highlightColourId
, "highlight", "hilitecol");
43 registerColour (TextEditor::outlineColourId
, "outline", "outlinecol");
44 registerColour (TextEditor::shadowColourId
, "shadow", "shadowcol");
45 registerColour (CaretComponent::caretColourId
, "caret", "caretcol");
48 //==============================================================================
49 Component
* createNewComponent (JucerDocument
*)
51 return new TextEditor ("new text editor");
54 XmlElement
* createXmlFor (Component
* comp
, const ComponentLayout
* layout
)
56 XmlElement
* e
= ComponentTypeHandler::createXmlFor (comp
, layout
);
57 TextEditor
* te
= (TextEditor
*) comp
;
59 e
->setAttribute ("initialText", comp
->getProperties() ["initialText"].toString());
61 e
->setAttribute ("multiline", te
->isMultiLine());
62 e
->setAttribute ("retKeyStartsLine", te
->getReturnKeyStartsNewLine());
63 e
->setAttribute ("readonly", te
->isReadOnly());
64 e
->setAttribute ("scrollbars", te
->areScrollbarsShown());
65 e
->setAttribute ("caret", te
->isCaretVisible());
66 e
->setAttribute ("popupmenu", te
->isPopupMenuEnabled());
71 bool restoreFromXml (const XmlElement
& xml
, Component
* comp
, const ComponentLayout
* layout
)
73 if (! ComponentTypeHandler::restoreFromXml (xml
, comp
, layout
))
76 TextEditor
* te
= (TextEditor
*) comp
;
77 TextEditor defaultEditor
;
79 te
->setMultiLine (xml
.getBoolAttribute ("multiline", defaultEditor
.isMultiLine()));
80 te
->setReturnKeyStartsNewLine (xml
.getBoolAttribute ("retKeyStartsLine", defaultEditor
.getReturnKeyStartsNewLine()));
81 te
->setReadOnly (xml
.getBoolAttribute ("readonly", defaultEditor
.isReadOnly()));
82 te
->setScrollbarsShown (xml
.getBoolAttribute ("scrollbars", defaultEditor
.areScrollbarsShown()));
83 te
->setCaretVisible (xml
.getBoolAttribute ("caret", defaultEditor
.isCaretVisible()));
84 te
->setPopupMenuEnabled (xml
.getBoolAttribute ("popupmenu", defaultEditor
.isPopupMenuEnabled()));
86 const String
initialText (xml
.getStringAttribute ("initialText"));
87 te
->setText (initialText
, false);
88 te
->getProperties().set ("initialText", initialText
);
92 void getEditableProperties (Component
* component
, JucerDocument
& document
, Array
<PropertyComponent
*>& properties
)
94 ComponentTypeHandler::getEditableProperties (component
, document
, properties
);
96 TextEditor
* const t
= dynamic_cast <TextEditor
*> (component
);
99 properties
.add (new TextEditorInitialTextProperty (t
, document
));
100 properties
.add (new TextEditorMultiLineProperty (t
, document
));
101 properties
.add (new TextEditorReadOnlyProperty (t
, document
));
102 properties
.add (new TextEditorScrollbarsProperty (t
, document
));
103 properties
.add (new TextEditorCaretProperty (t
, document
));
104 properties
.add (new TextEditorPopupMenuProperty (t
, document
));
106 addColourProperties (t
, document
, properties
);
109 const String
getCreationParameters (Component
* component
)
111 return quotedString (component
->getName());
114 void fillInCreationCode (GeneratedCode
& code
, Component
* component
, const String
& memberVariableName
)
116 ComponentTypeHandler::fillInCreationCode (code
, component
, memberVariableName
);
118 TextEditor
* const te
= dynamic_cast <TextEditor
*> (component
);
122 s
<< memberVariableName
<< "->setMultiLine (" << boolToString (te
->isMultiLine()) << ");\n"
123 << memberVariableName
<< "->setReturnKeyStartsNewLine (" << boolToString (te
->getReturnKeyStartsNewLine()) << ");\n"
124 << memberVariableName
<< "->setReadOnly (" << boolToString (te
->isReadOnly()) << ");\n"
125 << memberVariableName
<< "->setScrollbarsShown (" << boolToString (te
->areScrollbarsShown()) << ");\n"
126 << memberVariableName
<< "->setCaretVisible (" << boolToString (te
->isCaretVisible()) << ");\n"
127 << memberVariableName
<< "->setPopupMenuEnabled (" << boolToString (te
->isPopupMenuEnabled()) << ");\n"
128 << getColourIntialisationCode (component
, memberVariableName
)
129 << memberVariableName
<< "->setText (" << quotedString (te
->getProperties() ["initialText"].toString()) << ");\n\n";
131 code
.constructorCode
+= s
;
135 //==============================================================================
136 class TextEditorMultiLineProperty
: public ComponentChoiceProperty
<TextEditor
>
139 TextEditorMultiLineProperty (TextEditor
* component_
, JucerDocument
& document_
)
140 : ComponentChoiceProperty
<TextEditor
> ("mode", component_
, document_
)
142 choices
.add ("single line");
143 choices
.add ("multi-line, return key starts new line");
144 choices
.add ("multi-line, return key disabled");
147 //==============================================================================
148 void setIndex (int newIndex
)
150 document
.perform (new TextEditorMultilineChangeAction (component
, *document
.getComponentLayout(), newIndex
),
151 "Change TextEditor multiline mode");
156 return component
->isMultiLine() ? (component
->getReturnKeyStartsNewLine() ? 1 : 2) : 0;
160 class TextEditorMultilineChangeAction
: public ComponentUndoableAction
<TextEditor
>
163 TextEditorMultilineChangeAction (TextEditor
* const comp
, ComponentLayout
& layout
, const int newState_
)
164 : ComponentUndoableAction
<TextEditor
> (comp
, layout
),
167 oldState
= comp
->isMultiLine() ? (comp
->getReturnKeyStartsNewLine() ? 1 : 2) : 0;
173 getComponent()->setMultiLine (newState
> 0);
174 getComponent()->setReturnKeyStartsNewLine (newState
== 1);
182 getComponent()->setMultiLine (oldState
> 0);
183 getComponent()->setReturnKeyStartsNewLine (oldState
== 1);
188 int newState
, oldState
;
192 //==============================================================================
193 class TextEditorReadOnlyProperty
: public ComponentBooleanProperty
<TextEditor
>
196 TextEditorReadOnlyProperty (TextEditor
* component_
, JucerDocument
& document_
)
197 : ComponentBooleanProperty
<TextEditor
> ("editable", "Editable", "Editable", component_
, document_
)
201 //==============================================================================
202 void setState (bool newState
)
204 document
.perform (new TextEditorReadonlyChangeAction (component
, *document
.getComponentLayout(), ! newState
),
205 "Change TextEditor read-only mode");
208 bool getState() const { return ! component
->isReadOnly(); }
211 class TextEditorReadonlyChangeAction
: public ComponentUndoableAction
<TextEditor
>
214 TextEditorReadonlyChangeAction (TextEditor
* const comp
, ComponentLayout
& layout
, const bool newState_
)
215 : ComponentUndoableAction
<TextEditor
> (comp
, layout
),
218 oldState
= comp
->isReadOnly();
224 getComponent()->setReadOnly (newState
);
232 getComponent()->setReadOnly (oldState
);
237 bool newState
, oldState
;
241 //==============================================================================
242 class TextEditorScrollbarsProperty
: public ComponentBooleanProperty
<TextEditor
>
245 TextEditorScrollbarsProperty (TextEditor
* component_
, JucerDocument
& document_
)
246 : ComponentBooleanProperty
<TextEditor
> ("scrollbars", "Scrollbars enabled", "Scrollbars enabled", component_
, document_
)
250 //==============================================================================
251 void setState (bool newState
)
253 document
.perform (new TextEditorScrollbarChangeAction (component
, *document
.getComponentLayout(), newState
),
254 "Change TextEditor scrollbars");
257 bool getState() const { return component
->areScrollbarsShown(); }
260 class TextEditorScrollbarChangeAction
: public ComponentUndoableAction
<TextEditor
>
263 TextEditorScrollbarChangeAction (TextEditor
* const comp
, ComponentLayout
& layout
, const bool newState_
)
264 : ComponentUndoableAction
<TextEditor
> (comp
, layout
),
267 oldState
= comp
->areScrollbarsShown();
273 getComponent()->setScrollbarsShown (newState
);
281 getComponent()->setScrollbarsShown (oldState
);
286 bool newState
, oldState
;
290 //==============================================================================
291 class TextEditorCaretProperty
: public ComponentBooleanProperty
<TextEditor
>
294 TextEditorCaretProperty (TextEditor
* component_
, JucerDocument
& document_
)
295 : ComponentBooleanProperty
<TextEditor
> ("caret", "Caret visible", "Caret visible", component_
, document_
)
299 //==============================================================================
300 void setState (bool newState
)
302 document
.perform (new TextEditorCaretChangeAction (component
, *document
.getComponentLayout(), newState
),
303 "Change TextEditor caret");
306 bool getState() const { return component
->isCaretVisible(); }
309 class TextEditorCaretChangeAction
: public ComponentUndoableAction
<TextEditor
>
312 TextEditorCaretChangeAction (TextEditor
* const comp
, ComponentLayout
& layout
, const bool newState_
)
313 : ComponentUndoableAction
<TextEditor
> (comp
, layout
),
316 oldState
= comp
->isCaretVisible();
322 getComponent()->setCaretVisible (newState
);
330 getComponent()->setCaretVisible (oldState
);
335 bool newState
, oldState
;
339 //==============================================================================
340 class TextEditorPopupMenuProperty
: public ComponentBooleanProperty
<TextEditor
>
343 TextEditorPopupMenuProperty (TextEditor
* component_
, JucerDocument
& document_
)
344 : ComponentBooleanProperty
<TextEditor
> ("popup menu", "Popup menu enabled", "Popup menu enabled", component_
, document_
)
348 //==============================================================================
349 void setState (bool newState
)
351 document
.perform (new TextEditorPopupMenuChangeAction (component
, *document
.getComponentLayout(), newState
),
352 "Change TextEditor popup menu");
355 bool getState() const { return component
->isPopupMenuEnabled(); }
358 class TextEditorPopupMenuChangeAction
: public ComponentUndoableAction
<TextEditor
>
361 TextEditorPopupMenuChangeAction (TextEditor
* const comp
, ComponentLayout
& layout
, const bool newState_
)
362 : ComponentUndoableAction
<TextEditor
> (comp
, layout
),
365 oldState
= comp
->isPopupMenuEnabled();
371 getComponent()->setPopupMenuEnabled (newState
);
379 getComponent()->setPopupMenuEnabled (oldState
);
384 bool newState
, oldState
;
388 //==============================================================================
389 class TextEditorInitialTextProperty
: public ComponentTextProperty
<TextEditor
>
392 TextEditorInitialTextProperty (TextEditor
* component_
, JucerDocument
& document_
)
393 : ComponentTextProperty
<TextEditor
> ("initial text", 10000, true, component_
, document_
)
396 //==============================================================================
397 void setText (const String
& newText
)
399 document
.perform (new TextEditorInitialTextChangeAction (component
, *document
.getComponentLayout(), newText
),
400 "Change TextEditor initial text");
403 const String
getText() const
405 return component
->getProperties() ["initialText"];
409 class TextEditorInitialTextChangeAction
: public ComponentUndoableAction
<TextEditor
>
412 TextEditorInitialTextChangeAction (TextEditor
* const comp
, ComponentLayout
& layout
, const String
& newState_
)
413 : ComponentUndoableAction
<TextEditor
> (comp
, layout
),
416 oldState
= comp
->getProperties() ["initialText"];
422 getComponent()->setText (newState
, false);
423 getComponent()->getProperties().set ("initialText", newState
);
431 getComponent()->setText (oldState
, false);
432 getComponent()->getProperties().set ("initialText", oldState
);
437 String newState
, oldState
;
443 #endif // __JUCER_TEXTEDITORHANDLER_JUCEHEADER__